/**
 * 用户业务操作
 */

const validator = require('validator');
const userModel = require('../models/user-info');
const userCode = require('../codes/user');

const user = {

	/**
	 * 创建用户
	 * @param  {object} user 用户信息
	 * @return {object}      创建结果
	 */
	async create(user) {
		let result = await userModel.create(user);
		return result;
	},

	/**
	 * 查找存在用户信息
	 * @param  {object} formData 查找的表单数据
	 * @return {object|null}      查找结果
	 */
	async getExistOne(formData) {
		let resultData = await userModel.getExistOne({
			'phone': formData.phone,
			'user_name': formData.userName
		});
		return resultData;
	},

	/**
	 * 登录业务操作
	 * @param  {object} formData 登录表单信息
	 * @return {object}          登录业务操作结果
	 */
	async signIn(formData) {
		let resultData = await userModel.getOneByPhoneAndPassword({
			'password': formData.password,
			'phone': formData.phone
		});
		return resultData;
	},

	/**
	 * 根据用户名查找用户业务操作
	 * @param  {string} userName 用户名
	 * @return {object|null}     查找结果
	 */
	async getUserInfoByUserName(userName) {
		let resultData = await userModel.getUserInfoByUserName(userName) || {};
		let userInfo = {
			id: resultData.id,
			userName: resultData.user_name,
			phone: resultData.phone,
			createTime: resultData.create_time,
			updateTime: resultData.update_time,
		};
		return userInfo;
	},

	/**
	 * 检验用户注册数据
	 * @param  {object} userInfo 用户注册数据
	 * @return {object}          校验结果
	 */
	validatorSignUp(userInfo) {
		let result = {
			success: false,
			message: '',
		};

		if(/[a-z0-9\_\-]{6,16}/.test(userInfo.userName) === false) {
			result.message = userCode.ERROR_USER_NAME;
			return result;
		}
		if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(userInfo.phone))) {
			result.message = userCode.ERROR_PHONE;
			return result;
		}
		if(!/[\w+]{6,16}/.test(userInfo.password)) {
			result.message = userCode.ERROR_PASSWORD;
			return result;
		}
		if(userInfo.password !== userInfo.confirmPassword) {
			result.message = userCode.ERROR_PASSWORD_CONFORM;
			return result;
		}

		result.success = true;

		return result;
	}

};

module.exports = user;